package logic

import (
	"backend/apps/book/models"
	"context"
	"database/sql"
	"time"

	"backend/apps/book/proto"
	"backend/apps/book/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateBookLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateBookLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateBookLogic {
	return &CreateBookLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// CreateBook 书籍相关操作
func (l *CreateBookLogic) CreateBook(in *proto.Book) (*proto.CommonResponse, error) {
	now := time.Now().Unix()
	_, err := l.svcCtx.BookModel.Insert(l.ctx, &models.Book{
		AuthorId: in.AuthorId,
		Title:    in.Title,
		CoverUrl: in.CoverUrl,
		Summary: sql.NullString{
			String: in.Summary,
			Valid:  true,
		},
		TotalWords: in.TotalWords,
		Status:     int64(in.Status),
		IsVip:      int64(in.IsVip),
		CreateTime: now,
		UpdateTime: now,
	})
	return &proto.CommonResponse{}, err
}
